package com.gjy.util.sb.aop.aspect;

import com.gjy.util.sb.aop.annotation.NeedDataPermission;
import com.gjy.util.sb.aop.dto.DataPermission;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-10-23 11:05:21
 */
@Aspect
@Component
public class NeedDataPermissionAspect {

    private static final Logger log = LoggerFactory.getLogger(NeedDataPermissionAspect.class);

    @Before("@annotation(permission) && args(dataPermission)")
    public void datePermission(NeedDataPermission permission,
                               DataPermission dataPermission) {
        if (Objects.isNull(dataPermission) || Objects.isNull(dataPermission.getUserId())) {
            log.info("未初始化对象或未设置用户编号: {}", dataPermission);
            return;
        }
        prepareDataPermission(dataPermission, permission.needA(), permission.needB());
    }

    private void prepareDataPermission(DataPermission permission, boolean a, boolean b) {
        String userId = permission.getUserId();
        if (userId.equals("1")) { // 此处可以从数据库中查
            permission.setAll(true);
        }
        if (a) {
            // 从数据库中查数据进行赋值
            permission.setA("A");
        }
        if (b) {
            permission.setB("B");
        }
    }
}
